package com.zq.solr.test.service;

import com.zq.solr.sys.MyQueryParserBase;
import com.zq.solr.test.doman.Classes;
import com.zq.solr.test.doman.School;
import com.zq.solr.test.doman.User;
import com.zq.solr.test.main.TestMain;
import com.zq.solr.util.SolrUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.solr.core.DefaultQueryParser;
import org.springframework.data.solr.core.QueryParser;
import org.springframework.data.solr.core.QueryParserBase;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.mapping.SimpleSolrMappingContext;
import org.springframework.data.solr.core.query.*;
import org.springframework.data.solr.core.query.result.SolrResultPage;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.Calendar;
import java.util.List;

/**
 * Created by system.out on 2017/3/16.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestMain.class)
@WebAppConfiguration
public class SolrService {
    @Autowired
    private SolrTemplate solrTemplate;
    @Test
    public void save(){
        User user = new User();
        user.setId(1);
        user.setUsername("zhangsan");
        user.setPassword("123456");
        user.setClassesId(2);
        User user1 = new User();
        user1.setId(2);
        user1.setUsername("lisi");
        user1.setPassword("123456");
        user1.setClassesId(4);
        Classes classes = new Classes();
        classes.setId(1);
        classes.setClassesName("一班");
        classes.setSchoolId(1);
        Classes classes1 = new Classes();
        classes1.setId(2);
        classes1.setClassesName("二班");
        classes1.setSchoolId(1);
        Classes classes2 = new Classes();
        classes2.setId(3);
        classes2.setClassesName("三班");
        classes2.setSchoolId(2);
        School school = new School();
        school.setId(1);
        school.setSchoolName("第一学院");
        School school1 = new School();
        school1.setId(2);
        school1.setSchoolName("第二学院");
        solrTemplate.saveBean(user);
        solrTemplate.saveBean(user1);
        solrTemplate.saveBean(classes);
        solrTemplate.saveBean(classes1);
        solrTemplate.saveBean(classes2);
        solrTemplate.saveBean(school);
        solrTemplate.saveBean(school1);
        solrTemplate.commit(SolrUtils.getSolrCoreName(User.class));
        solrTemplate.commit(SolrUtils.getSolrCoreName(Classes.class));
        solrTemplate.commit(SolrUtils.getSolrCoreName(School.class));
    }
    @Test
    public void query(){
        Query query = new SimpleQuery();
        query.setOffset(0);
        query.setRows(10);
//        query.addCriteria(AnyCriteria.any());
        Criteria criteria = new Criteria("id").is(1);
        query.addCriteria(criteria);
        FilterQuery filterQuery = new SimpleFilterQuery();
        query.addFilterQuery(filterQuery);
        Join join =  Join.from("id").fromIndex(SolrUtils.getSolrCoreName(Classes.class)).to("classesId");
        Join join1 =  Join.from("id").fromIndex(SolrUtils.getSolrCoreName(School.class)).to("schoolId");
        query.setJoin(join);
        query.setJoin(join1);
        query.addProjectionOnField(new SimpleField("id"));
        QueryParser parser = new DefaultQueryParser();
        System.out.println(parser.constructSolrQuery(query).toQueryString());
        SolrResultPage<User> resultPage =solrTemplate.query(query,User.class);
        System.out.println(resultPage.getContent().size());
        for(User user : resultPage.getContent()){
            System.out.println(user.getId()+"&"+user.getUsername()+"&"+user.getPassword()+"&"+user.getClassesId());
        }
    }

}
